var tgd = tgd || {};
tgd.Contests = tgd.Contests || {};
tgd.Contests.Darts = tgd.Contests.Darts || {};
tgd.Contests.Darts.Views = tgd.Contests.Darts.Views || {};

tgd.Contests.Darts.Views.AllScores = function (canvas, numItems) {
	this._canvas = canvas;
	this._table = null;
	this._numItems = numItems;  //todo:  make configurable
	this._solutions = [];
	this._callbacks = null;

	var tempRow = null;

    this._table = document.createElement("table");
	this._table.createTHead();
	tempRow = this._table.tHead.insertRow(0);
	for (var i = 0; i < 3; ++i, this._table.tHead.rows[0].appendChild(document.createElement("th")));
	tempRow.cells[0].innerHTML = "Score";
	tempRow.cells[1].innerHTML = "Time";
	tempRow.cells[2].innerHTML = "Solution";

    for (var i = 0; i < this._numItems; ++i) {
        tempRow = this._table.tBodies[0].insertRow(i);
		for (var j = 0; j < 3; tempRow.insertCell(j++));
    }

    this._canvas.appendChild(this._table);

	if (tgd.Contests.Darts.Views.AllScores._initialized === undefined) {

		tgd.Contests.Darts.Views.AllScores.prototype.onUpdate = function(s) {

			for (var i = this._numItems - 1; i > 0; this._solutions[--i] = this._solutions[i]);
			this._solutions[0] = s;

			//todo:  not a fan of tbody junk
			this._table.tBodies[0].deleteRow(this._numItems - 1);
			var row = this._table.tBodies[0].insertRow(0);
			for (var j = 0; j < 3; row.insertCell(j++));
			row.cells[0].innerHTML = s.getSmallestScoreUnattainable();
			row.cells[1].innerHTML = new Date();
			row.cells[2].innerHTML = s + "    " + this._canvas.id;
		}

		/*
		 * c - { onStart, onStop }
		 */
		tgd.Contests.Darts.Views.AllScores.prototype.registerCallback = function(c) {
			this._callbacks = c;
			var self = this;
			this._canvas.ondblclick = function() { self._callbacks.onStop();}
			this._canvas.onclick = function() { self._callbacks.onStart();}
		}

		tgd.Contests.Darts.Views.AllScores._initialized = true;
	}
};